﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;
using Tanmia.Basis;

namespace SaryaAccounting
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        private AppBootstrapper bootstrapper;

        public static string Mohammad { get; set; }
        public static void Hi<T>(System.Linq.Expressions.Expression<Func<T>> myParm)
        {
            Type t = typeof(T);
        }

        protected override void OnStartup(StartupEventArgs e)
        {
            //SecurityAdapterInitializer.GenerateDatabase(null);
            //SecurityAdapterInitializer.AddAdminUser();

            //SecurityAdapterInitializer.ConfigureSecurity(bootstrapper.Container);

            //            Hi(() => Mohammad);

            base.OnStartup(e);
            bootstrapper = new AppBootstrapper();
#if (DEBUG)
            RunInDebugMode();
#else
           RunInReleaseMode();
#endif
            ShutdownMode = System.Windows.ShutdownMode.OnMainWindowClose;
        }

        private void RunInDebugMode()
        {
            bootstrapper.User = new ApplicationUserInfo { Username = "admin", Password = "admin" };
            bootstrapper.Run(false);
        }

        private void RunInReleaseMode()
        {
            bootstrapper.Run(true);
            AppDomain.CurrentDomain.UnhandledException += AppDomainUnhandledException;
        }

        private void AppDomainUnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            HandleException(e.ExceptionObject as Exception);
        }

        private void HandleException(Exception exception)
        {
            if (exception == null)
                return;
            MessageBox.Show(SaryaAccounting.Properties.Resources.UnhandledException);
            Environment.Exit(1);
        }
    }
}
